<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Contract;
use App\Models\Project;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Models\Administrator;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Widgets\Card;
use Dcat\Admin\Widgets\Markdown;

class ContractController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Contract(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('code');
            $grid->column('pro_id')->display(function ($pro_id){
                return Project::query()->where('id',$pro_id)->value('name');
            });
            //content是editor格式，如何正确显示


            $grid->column('content')->display(function ($content){
                return Markdown::make($content);
            })->width('20%');
            $grid->column('href','站外链接')->limit(20,'...')->link();
            $grid->column('admin_id')->display(function ($admin_id){
                return Administrator::query()->where('id',$admin_id)->value('username');
            })->label('primary');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Contract(), function (Show $show) {
            $show->field('id');
            $show->field('code');
            $show->field('pro_id');
            $show->field('content');
            $show->field('href');
            $show->field('admin_id');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Contract(), function (Form $form) {
            $form->display('id');
//            $form->text('code')->default();
            //合同编号，自动生成6位大写字母加6位数字
            $form->text('code')->default(function(){
                $code = strtoupper(substr(md5(uniqid(mt_rand(), true)), 0, 6));
                $num = mt_rand(100000, 999999);
                return $code.$num;
            });
            $form->html(
                '<button type="button" class="generate-code btn btn-sm btn-default btn-success">重新生成验证码</button>'
            );
            $script = <<<EOT
                    $('.generate-code').on('click', function() {
                        var codeField = $('input[name="code"]');
                        var code = Array.from({length: 6}, () => String.fromCharCode(65 + Math.floor(Math.random() * 26))).join('');
                        var num = Math.floor(Math.random() * (999999 - 100000 + 1)) + 100000;
                        codeField.val(code + num);
                    });
                    EOT;

// 注入到页面底部的JavaScript中
            Admin::script($script);

            $form->select('pro_id')->options(function () {

                $project = Project::pluck('name', 'id');

                if ($project) {
                    return $project;
                }
            });
            $form->editor('content');
            $form->hidden('href');
            $form->hidden('admin_id')->default(auth('admin')->user()->id);
//            $form->tools(function (Form\Tools $tools) {
//                $tools->append('<a href="javascript:;" class="btn btn-sm btn-default generate-code">重新生成验证码</a>');
//            });
            $form->display('created_at');
            $form->display('updated_at');
        })->saving(function (Form $form) {
            $form->href=url('contract/'.$form->code);
        });
    }
}
